import { getPaymentOrders, pay, closePaymentOrder } from '@/api/payment-order'
import Pagination from '@/components/Pagination/index'
import { getSales } from '@/api/salesman'

export default {
  name: 'Payment-Order',
  components: { Pagination },

  data(){
    return{
      list: null,
      statistics: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        pay_state: null,
        state: null,
        operation: null,
        salesman: null,
        from_date: null,
        to_date: null,
        pay_from_date: null,
        pay_to_date: null
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      datetime: null,
      pay_datetime: null,
      sales_options:[
        {
          value: '',
          label: '不限'
        }
      ],
      operation_options:[
        {
          value: '',
          label: '不限'
        },
        {
          value: 'RETURNED',
          label: '售后退款'
        },
        {
          value:'COMMISSION',
          label: '会员返佣'
        }
      ],
      pay_options:[
        {
          value: '',
          label: '不限'
        },
        {
          value: 'UNPAID',
          label: '未付'
        },
        {
          value:'PAID',
          label: '已付'
        },
        {
          value:'FAIL',
          label: '付款失败'
        }
      ],
      state_options:[
        {
          value: '',
          label: '不限'
        },
        {
          value: 'NORMAL',
          label: '正常'
        },
        {
          value:'FINISHED',
          label: '已完成'
        },
        {
          value:'CLOSED',
          label: '已关闭'
        }
      ],
      salesman:null
    }
  },

  created(){
    this.getList()
    this.getSales()
  },

  methods:{
    getSales: async function() {
      const options = this.sales_options
      const res = await getSales();
      for(const v of res.list){
        options.push({
          value:v.id,
          label:v.nickname
        })
      }
    },
    getList() {
      this.listLoading = true
      getPaymentOrders(this.listQuery).then(response => {
        this.list = response.list
        this.total = response.total
        this.statistics = response.statistics
        this.listLoading = false
      }).catch(error => {
        this.list = []
        this.total = 0
        this.listLoading = false
        Promise.reject(error)
      })
    },

    handleSearch() {
      if (this.listQuery.salesman == '') {
        this.listQuery.salesman = null
      }
      if (this.listQuery.operation == '') {
        this.listQuery.operation = null
      }
      if (this.listQuery.state == '') {
        this.listQuery.state = null
      }
      if (this.listQuery.pay_state == '') {
        this.listQuery.pay_state = null
      }
      if (!this.datetime) {
        this.listQuery.from_date = null
        this.listQuery.to_date = null
      } else {
        this.listQuery.from_date = this.datetime[0]
        this.listQuery.to_date = this.datetime[1]
      }
      if (!this.pay_datetime) {
        this.listQuery.pay_from_date = null
        this.listQuery.pay_to_date = null
      } else {
        this.listQuery.pay_from_date = this.pay_datetime[0]
        this.listQuery.pay_to_date = this.pay_datetime[1]
      }


      this.list = []
      this.total = 0
      this.getList()
    },

    getSummaries(){
      if(!this.statistics){
        return
      }
      const statistics = this.statistics
      const sums = []
      sums[4] = statistics.total_amount

      return sums
    },

    pay(id,amount){
      if(amount > 600){
        this.$notify.error("超过600需在商户平台手动打款")
        return
      }

      const loading = this.$loading({
        lock: true,
        text: '正在付款',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

      let self = this
      pay(id).then(res => {
        if(res.status == 'SUCCESS'){
          self.$notify.success("付款成功")
          loading.close()
          self.handleSearch()
        }else{
          self.$notify.error('付款失败')
          loading.close()
          self.handleSearch()
        }
      })
    },

    closeOrder(id){
      const loading = this.$loading({
        lock: true,
        text: '正在关闭',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      closePaymentOrder(id).then(res => {
        self.$notify.success("关闭成功")
        loading.close()
        self.handleSearch()
      })
    }

  }
}
